#include "tencentaccount.h"

#include "tencentmicroblog.h"

#include <choqok/passwordmanager.h>
#include <QtOAuth/QtOAuth>

static const char tencentConsumerKey[] = "f2d56411e41e468ebcba8a52ddec45a4";
static const char tencentConsumerSecret[] = "f1e645c913e0b3f68ea376d6f35a6a36";

TencentAccount::TencentAccount( TencentMicroBlog* parent, const QString& alias )
: Choqok::Account(parent,alias)
{
    m_oauthToken = configGroup()->readEntry( QString( "%1_OAuthToken" ).arg( alias ), QByteArray() );
    m_oauthTokenSecret = Choqok::PasswordManager::self()->readPassword( QString( "%1_OAuthTokenSecret" ).arg( alias ) ).toUtf8();
    clientip = "127.0.0.1";
    /// TODO KDE 4.5 Change to use new class
    qoauth = new QOAuth::Interface;//( new KIO::AccessManager( this ), this );
    qoauth->setConsumerKey( TencentAccount::oauthConsumerKey() );
    qoauth->setConsumerSecret( TencentAccount::oauthConsumerSecret() );
    qoauth->setRequestTimeout( 10000 );
    qoauth->setIgnoreSslErrors( true );
}

TencentAccount::~TencentAccount()
{
    delete qoauth;
}

void TencentAccount::writeConfig()
{
    configGroup()->writeEntry( QString( "%1_OAuthToken" ).arg( alias() ), m_oauthToken );
    Choqok::PasswordManager::self()->writePassword( QString( "%1_OAuthTokenSecret" ).arg( alias() ),
                                                    QString::fromUtf8( m_oauthTokenSecret ) );
    Choqok::Account::writeConfig();
}

const QByteArray TencentAccount::oauthConsumerKey()
{
    return tencentConsumerKey;
}

const QByteArray TencentAccount::oauthConsumerSecret()
{
    return tencentConsumerSecret;
}

QOAuth::Interface* TencentAccount::qoauthInterface() const
{
    return qoauth;
}

void TencentAccount::setOauthToken( const QByteArray& token )
{
    m_oauthToken = token;
}

const QByteArray TencentAccount::oauthToken() const
{
    return m_oauthToken;
}

void TencentAccount::setOauthTokenSecret( const QByteArray& tokenSecret )
{
    m_oauthTokenSecret = tokenSecret;
}

const QByteArray TencentAccount::oauthTokenSecret() const
{
    return m_oauthTokenSecret;
}
